package main

import (
	"encoding/json"
	"fmt"
	"net/http"
)

func main() {

	http.HandleFunc("/jsonp", func(w http.ResponseWriter, r *http.Request) {
		callback := r.URL.Query().Get("jsonp")

		w.Header().Set("Content-Type", "application/javascript")

		resp := struct {
			Message string `json:"msg"`
		}{"Hello, golang jsonp!"}

		res, err := json.Marshal(resp)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		fmt.Println(resp)

		w.Write([]byte(callback + "("))
		w.Write(res)
		w.Write([]byte(");"))
	})

	fmt.Println("Go server running...")
	http.ListenAndServe(":5501", nil)
}
